namespace ACRSAP.Administration.Projects.UserControls
{
    using ASPNET.StarterKit.IssueTracker.BusinessLogicLayer;
    using ASPNET.StarterKit.IssueTracker.UserInterfaceLayer;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using ASPNET.UserControls;

    public class ProjectStatus : UserControl, IEditProjectControl
    {
        [AccessedThroughProperty("btnAdd")]
        private Button _btnAdd;
        [AccessedThroughProperty("CustomValidator1")]
        private CustomValidator _CustomValidator1;
        [AccessedThroughProperty("grdStatus")]
        private DataGrid _grdStatus;
        [AccessedThroughProperty("lblError")]
        private Label _lblError;
        [AccessedThroughProperty("lstImages")]
        private PickImage _lstImages;
        private int _ProjectId;
        [AccessedThroughProperty("txtName")]
        private TextBox _txtName;
        private object designerPlaceholderDeclaration;

        public ProjectStatus()
        {
            base.Init += new EventHandler(this.Page_Init);
            this._ProjectId = -1;
        }

        public void AddStatus(object s, EventArgs e)
        {
            string newName = this.txtName.Text.Trim();
            if (StringType.StrCmp(newName, string.Empty, false) != 0)
            {
                Status newStatus = new Status(this.ProjectId, newName, this.lstImages.SelectedValue);
                if (newStatus.Save())
                {
                    this.txtName.Text = "";
                    this.BindStatus();
                    this.lstImages.SelectedValue = string.Empty;
                }
                else
                {
                    this.lblError.Text = "Could not save status";
                }
            }
        }

        public void BindStatus()
        {
            this.grdStatus.DataSource = Status.GetStatusByProjectId(this.ProjectId);
            this.grdStatus.DataKeyField = "Id";
            this.grdStatus.DataBind();
            if (this.grdStatus.Items.Count == 0)
            {
                this.grdStatus.Visible = false;
            }
            else
            {
                this.grdStatus.Visible = true;
            }
        }

        public void DeleteStatus(object s, DataGridCommandEventArgs e)
        {
            if (!Status.DeleteStatus(IntegerType.FromObject(this.grdStatus.DataKeys[e.Item.ItemIndex])))
            {
                this.lblError.Text = "Could not delete status";
            }
            else
            {
                this.BindStatus();
            }
        }

        public void grdStatus_ItemDataBound(object s, DataGridItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) | (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                Status currentStatus = (Status) e.Item.DataItem;
                Label lblStatusName = (Label) e.Item.FindControl("lblStatusName");
                lblStatusName.Text = currentStatus.Name;
                Image imgStatus = (Image) e.Item.FindControl("imgStatus");
                if (StringType.StrCmp(currentStatus.ImageUrl, string.Empty, false) == 0)
                {
                    imgStatus.Visible = false;
                }
                else
                {
                    imgStatus.ImageUrl = "~/ACR/Images/Status/" + currentStatus.ImageUrl;
                    imgStatus.AlternateText = currentStatus.Name;
                }
                Button btnDelete = (Button) e.Item.FindControl("btnDelete");
                btnDelete.Attributes.Add("onclick", string.Format("return confirm('Are you sure you want to delete the \"{0}\" status?');", currentStatus.Name));
            }
        }

        public void Initialize()
        {
            this.BindStatus();
            this.lstImages.Initialize();
        }

        [DebuggerStepThrough]
        private void InitializeComponent()
        {
        }

        private void Page_Init(object sender, EventArgs e)
        {
            this.InitializeComponent();
        }

        public bool Update()
        {
            return this.Page.IsValid;
        }

        public void ValidateStatus(object s, ServerValidateEventArgs e)
        {
            if (this.grdStatus.Items.Count > 0)
            {
                e.IsValid = true;
            }
            else
            {
                e.IsValid = false;
            }
        }

        int ASPNET.StarterKit.IssueTracker.UserInterfaceLayer.IEditProjectControl.ProjectId
        {
            get
            {
                return this._ProjectId;
            }
            set
            {
                this._ProjectId = value;
            }
        }

        protected virtual Button btnAdd
        {
            get
            {
                return this._btnAdd;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._btnAdd != null)
                {
                    this._btnAdd.Click -= new EventHandler(this.AddStatus);
                }
                this._btnAdd = value;
                if (this._btnAdd != null)
                {
                    this._btnAdd.Click += new EventHandler(this.AddStatus);
                }
            }
        }

        protected virtual CustomValidator CustomValidator1
        {
            get
            {
                return this._CustomValidator1;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._CustomValidator1 != null)
                {
                    this._CustomValidator1.ServerValidate -= new ServerValidateEventHandler(this.ValidateStatus);
                }
                this._CustomValidator1 = value;
                if (this._CustomValidator1 != null)
                {
                    this._CustomValidator1.ServerValidate += new ServerValidateEventHandler(this.ValidateStatus);
                }
            }
        }

        protected virtual DataGrid grdStatus
        {
            get
            {
                return this._grdStatus;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._grdStatus != null)
                {
                    this._grdStatus.ItemDataBound -= new DataGridItemEventHandler(this.grdStatus_ItemDataBound);
                    this._grdStatus.DeleteCommand -= new DataGridCommandEventHandler(this.DeleteStatus);
                }
                this._grdStatus = value;
                if (this._grdStatus != null)
                {
                    this._grdStatus.ItemDataBound += new DataGridItemEventHandler(this.grdStatus_ItemDataBound);
                    this._grdStatus.DeleteCommand += new DataGridCommandEventHandler(this.DeleteStatus);
                }
            }
        }

        protected virtual Label lblError
        {
            get
            {
                return this._lblError;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._lblError != null)
                {
                }
                this._lblError = value;
                if (this._lblError != null)
                {
                }
            }
        }

        protected virtual PickImage lstImages
        {
            get
            {
                return this._lstImages;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._lstImages != null)
                {
                }
                this._lstImages = value;
                if (this._lstImages != null)
                {
                }
            }
        }

        public int ProjectId
        {
            get
            {
                return this._ProjectId;
            }
            set
            {
                this._ProjectId = value;
            }
        }

        protected virtual TextBox txtName
        {
            get
            {
                return this._txtName;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                if (this._txtName != null)
                {
                }
                this._txtName = value;
                if (this._txtName != null)
                {
                }
            }
        }
    }
}

